
/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      // Failed to create required ActiveXObject
      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }
  return xmlreq;
}

function getXmlHandler(req, responseXmlHandler) {
  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {
    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {
        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);
      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function getTextHandler(req, responseTextHandler) {
  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {
    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {
        // Pass the XML payload of the response to the 
        // handler function
        responseTextHandler(req.responseText);
      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function doKeyUp()
{
            var key = document.getElementById("key").value;

            if(key.length>0) 
            {
                var req = newXMLHttpRequest();

				 var handlerFunction = getTextHandler(req, showNotes);
				 req.onreadystatechange = handlerFunction;
				req.Open("POST","/SEARCH?key=" + key,true);//POST
              //  xmlhttpRequest.onreadystatechange=stateChange; 
                req.Send();
            }
}

		
function showNotes(responseStr)
{
	if(responseStr.length>0) 
   {
	
        array=responseStr.split('|') 
		positionDiv(); 
		document.getElementById("notes").innerHTML=""; 
        for(var i=0;i<array.length;i++)
        {
            if(array[i]!="") 
                {
                   document.getElementById("notes").innerHTML+="<div id='item" + i + "' class='itemBg' onmouseover='beMouseOver(" + i +")' onmouseout='beMouseOut(" + i + ")' onclick='beClick(" + i + ")'>" + array[i] + "</div>";
                }
        }

        document.getElementById("notes").innerHTML+="<div class='item_button' onclick='hiddenDiv();'><font color='#999999'>�ر�</font></div>";
        document.getElementById("notes").style.display="inline";
    }
	else
    {
        document.getElementById("notes").style.display="none";
    }
					
             
}

function positionDiv()
{
    var DivRef= document.getElementById("notes");
    DivRef.style.position = "absolute";
    var t=document.getElementById("key");
    DivRef.style.top= getAbsolutePos(t).y+25; 
    DivRef.style.left= getAbsolutePos(t).x ; 
    DivRef.style.height=array.length * 20; 
}
 
function hiddenDiv()
{
    document.getElementById("notes").style.display="none";
}
 
function getAbsolutePos(el)
{
    var SL = 0, ST = 0;
    var is_div = /^div$/i.test(el.tagName);
    if (is_div && el.scrollLeft) SL = el.scrollLeft;
    if (is_div && el.scrollTop) ST = el.scrollTop;
    var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
    if (el.offsetParent)
    {
                var tmp = this.getAbsolutePos(el.offsetParent);
                r.x += tmp.x;
                r.y += tmp.y;
    }
    return r;
}


     
function beMouseOverEFF(i)
{
    if ((i>=0)&(i<=array.length-1))
    {
        document.getElementById("item" + i).className="item_high";
    }
}

 
        function beMouseOutEFF(i)
        {
            if ((i>=0)&(i<=array.length-1))
            {
                document.getElementById("item" + i).className="item_normal";
            }
        }

 
        function beMouseOver(i)
        {
            document.getElementById("key").focus();
            beMouseOutEFF(zz);
            zz=i;
            beMouseOverEFF(zz);
        }

 
        function beMouseOut(i)
        {
            beMouseOutEFF(i);
        }
 
        function beClick(i)
		{
            document.getElementById("key").value=array[i];
            document.getElementById("notes").style.display="none";
            document.getElementById("key").focus();
        }

